clear all
cd "I:\workdata\706727\Build\Data"
use psychighfreq_AllAges_finalfull 

cap drop dsui
gen dsui=(c_liste49=="48")
keep pnr year month koen efalle famany* agec kom erp_* st_phosp_* bamb_* erp_* Dpsyc p_* Dtherapy firsttherapy Dpsyk_test Dpsychiatry  ///
	psyk_test psychiatry psyc therapy birthyear Dsui asui dsui mentaldiag adep anxiety ///
	Dpsykmed Danxiety Dsleep Danxsleep Dadep Dssri Dpsychostim Dantipsych first* Dpois Dpois_painkiller  DepSev prescrib

gen cohab=(!mi(efalle))
	
gen byte Dbamb=(!mi(bamb_1))
gen byte Derp=(!mi(erp_1))
gen byte DPHosp=(!mi(st_phosp_1))
gen byte Dbamb_AnxDep=(bamb_1==3 | bamb_2==3 | bamb_1==4 | bamb_2==4)
gen byte Derp_AnxDep=(erp_1==3 | erp_2==3 | erp_1==4 | erp_2==4)
gen byte DPHosp_AnxDep=(st_phosp_1==3 | st_phosp_2==3 | st_phosp_1==4 | st_phosp_2==4)
gen byte DAnxDep=(Dbamb_AnxDep==1 |DPHosp_AnxDep==1 | Derp_AnxDep==1)
gen byte firstDPHosp_AnxDep=firstpsykhosp*DPHosp_AnxDep==1
gen byte firstDerp_AnxDep=firstpsykhosp*Derp_AnxDep==1
gen byte firstDbamb_AnxDep=firstpsykhosp*Dbamb_AnxDep==1
gen byte firstpsykhospAnxDep=firstpsykhosp*(Dbamb_AnxDep==1 | Derp_AnxDep==1 | DPHosp_AnxDep==1)

local i=1
foreach var in firstadep firstssri firstanxiety firstsleep  {
gen byte prim_`var'=(substr(prescrib,`i',1)=="1") & `var'==1
gen byte phych_`var'=(substr(prescrib,`i',1)=="2") & `var'==1
gen byte sec_`var'=(substr(prescrib,`i',1)=="3") & `var'==1
local i=`i'+1
}
gen byte prim_firstanxsleep=(prim_firstanxiety==1 | prim_firstsleep==1)
gen byte phych_firstanxsleep=(phych_firstanxiety==1 | phych_firstsleep==1) & prim_firstanxsleep!=1
gen byte sec_firstanxsleep=(sec_firstanxiety==1 | sec_firstsleep==1) & prim_firstanxsleep!=1 & phych_firstanxsleep!=1
drop prim_firstssri phych_firstssri sec_firstssri

forvalues l=0/10 {
cap drop firstpsykhosp_`l'
gen byte firstpsykhosp_`l'=firstpsykhosp*(bamb_1==`l' | bamb_2==`l' | erp_1==`l' | erp_2==`l' | st_phosp_1==`l' | st_phosp_2==`l')
}
forvalues l=0/10 {
cap drop Dpsykhosp_`l'
gen byte Dpsykhosp_`l'=(bamb_1==`l' | bamb_2==`l' | erp_1==`l' | erp_2==`l' | st_phosp_1==`l' | st_phosp_2==`l')
}

gen year2=year if inrange(month,4,12)
replace year2=year-1 if inrange(month,1,3)

sort pnr year month

cap drop cyear
gen cyear=year+(month-1)/12

replace asui=0 if mi(asui) | asui<0
replace dsui=0 if mi(dsui) | dsui<0
gen sui=asui+dsui
replace p_dsui=p_dsui*dsui
replace p_asui=0 if mi(p_asui) | p_asui<0
replace p_dsui=0 if mi(p_dsui) | p_dsui<0
gen p_sui=p_asui+p_dsui

/* Convert to 2020-prices using price of a doctor's visit  */
foreach var in p_psyk_test p_psychiatry p_therapy p_psyc  {
replace `var'=`var'/100 if year<=2004
}
foreach var in p_psyk_test p_psychiatry p_therapy p_psyc p_adep p_anxiety {
replace `var'=0 if mi(`var') | `var'<0


replace `var'=`var'*(145.46/97.11) if year2==2000
replace `var'=`var'*(145.46/100.35) if year2==2001
replace `var'=`var'*(145.46/102.59) if year2==2002
replace `var'=`var'*(145.46/105.57) if year2==2003
replace `var'=`var'*(145.46/106.81) if year2==2004
replace `var'=`var'*(145.46/108.05) if year2==2005
replace `var'=`var'*(145.46/113.25) if year2==2006
replace `var'=`var'*(145.46/116.90) if year2==2007
replace `var'=`var'*(145.46/123.02) if year2==2008
replace `var'=`var'*(145.46/126.86) if year2==2009
replace `var'=`var'*(145.46/128.76) if year2==2010
replace `var'=`var'*(145.46/129.52) if year2==2011
replace `var'=`var'*(145.46/131.93) if year2==2012
replace `var'=`var'*(145.46/132.44) if year2==2013
replace `var'=`var'*(145.46/133.84) if year2==2014
}

replace erp_s=0 if mi(erp_s) | DAnxDep==0
replace bamb_s=0 if mi(bamb_s) | DAnxDep==0
replace st_phosp_s=0 if mi(st_phosp_s) | DAnxDep==0

gen psykhospAnxDep=erp_s+bamb_s+st_phosp_s
gen p_psykhospAnxDep=(erp_s+bamb_s)*1844+st_phosp_s*3686 /* 2018-price */
replace p_psykhospAnxDep=p_psykhospAnxDep*(145.46/142.22) /* Convert to 2020-price */

foreach var in p_psyk_test p_psychiatry p_therapy p_psyc p_sui p_adep p_anxiety {
replace `var'=0 if mi(`var')
}
sort pnr year month
foreach var in psykhospAnxDep psyc psyk_test psychiatry therapy  sui adep anxiety {
 by pnr: gen eps`var'=sum(first`var')
}
gen yrm=year+(month-1)/12
foreach var in psykhospAnxDep psyc psyk_test psychiatry therapy  sui adep anxiety {
sort pnr eps`var' year month
by pnr eps`var': gen minyrm=yrm[1]
by pnr eps`var': gegen count`var'=sum(`var') if yrm-minyrm<=3
by pnr eps`var': gegen pcount`var'=sum(p_`var') if yrm-minyrm<=3
replace count`var'=0 if first`var'!=1
replace pcount`var'=0 if first`var'!=1
drop minyrm
}

* Winzorize *
foreach var in psykhospAnxDep psyc psyk_test psychiatry therapy  sui adep anxiety {
qui sum count`var' if count`var'>0 & !mi(count`var'), d
replace count`var'=r(p99) if count`var'>r(p99) & !mi(count`var')

qui sum pcount`var' if pcount`var'>0 & !mi(pcount`var'), d
replace pcount`var'=r(p99) if pcount`var'>r(p99) & !mi(pcount`var')
}

destring(kom),replace

keep koen agec kom cohab famany* pcount* count* first* Dpsykhosp* Dpsyc Dtherapy Dpsyk_test Dpsychiatry  Dbamb Derp DPHosp Dbamb_AnxDep Derp_AnxDep DPHosp_AnxDep ///
 birthyear Dsui asui dsui mentaldiag Dpsykmed Danxiety Dsleep Danxsleep Dadep Dssri Dpsychostim Dantipsych Dpois Dpois_painkiller DepSev prim_* phych_* sec_* pnr year2
collapse (max) koen maxagec=agec kom cohab famany* pcount* count* first* Dpsykhosp* Dpsyc Dtherapy Dpsyk_test Dpsychiatry Dbamb Derp DPHosp Dbamb_AnxDep Derp_AnxDep DPHosp_AnxDep ///
 birthyear Dsui asui dsui mentaldiag Dpsykmed Danxiety Dsleep Danxsleep Dadep Dssri Dpsychostim Dantipsych Dpois Dpois_painkiller DepSev prim_* phych_* sec_* (min) minagec=agec, by(pnr year2) fast
 
rename year2 year
drop if mi(year)

drop if year==2014

*drop minagec
*gen minagec=maxagec-1

save I:\workdata\706727\Build\Data\psychighfreq_AllAges_YearCollapse , replace
